package com.store.application.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;

import org.hibernate.Query;
import org.springframework.context.ApplicationContext;

import com.store.application.Company;
import com.store.application.Customer;
import com.store.application.Order;
import com.store.application.Product;
import com.store.application.Store;
import com.store.business.CompanyInDatabase;
import com.store.business.CustomerInDatabase;
import com.store.business.ProductOrder;
import com.store.business.ProductInStock;
import com.store.view.SpringContext;

public class StoreImpl implements Store{
	
	EntityManager entityManager;
	
	public StoreImpl(EntityManagerFactory entityManagerFactory){
		entityManager = entityManagerFactory.createEntityManager();
	}
	
	public List<Order> getAllOrders(){
		
		List liste = entityManager.createQuery( "SELECT p FROM ProductOrder p" ).getResultList();
		List<Order> orders = new ArrayList<Order>();
		Iterator iterator = liste.iterator();
		ProductOrder productOrder;
		Order order;
		while(iterator.hasNext()){
			productOrder = (ProductOrder)iterator.next();
			order = new Order();
			orders.add(order);
		}
		return orders;
		
		
	}
	
	
	

	public List<Company> getAllCompanies(){
		
		List liste = entityManager.createQuery( "SELECT p FROM CompanyInDatabase p" ).getResultList();
		List<Company> companies = new ArrayList<Company>();
		Iterator iterator = liste.iterator();
		CompanyInDatabase companyInDatabase;
		Company company;
		while(iterator.hasNext()){
			companyInDatabase = (CompanyInDatabase)iterator.next();
			company = new Company(companyInDatabase.getIdCompany(), companyInDatabase.getNameCompany(), companyInDatabase.getDescriptionCompany(),companyInDatabase.getSecteurActiviteCompany());
			companies.add(company);
		}
		return companies;
		
		
	}
	
	public List<Company> getCompanieByName(String companyName){		
				List liste = entityManager.createQuery( "SELECT p FROM CompanyInDatabase p" ).setParameter("name", companyName ).getResultList();
		List<Company> companies = new ArrayList<Company>();
		Iterator iterator = liste.iterator();
		CompanyInDatabase companyInDatabase;
		Company company;
		while(iterator.hasNext()){
			companyInDatabase = (CompanyInDatabase)iterator.next();
			company = new Company(companyInDatabase.getIdCompany(), companyInDatabase.getNameCompany(), companyInDatabase.getDescriptionCompany(),companyInDatabase.getSecteurActiviteCompany());
			companies.add(company);
		}
		return companies;
		
		
	}
	
	public List<Order> getOrderByDate(Date date){
		// todo
		return null;
	}

	
	
	@Override
	public List<Product> getListOfProducts() {
		List liste = entityManager.createQuery( "SELECT p FROM ProductInStock p" ).getResultList();
		List<Product> products = new ArrayList<Product>();
		Iterator iterator = liste.iterator();
		ProductInStock productInStock;
		Product product;
		while(iterator.hasNext()){
			productInStock = (ProductInStock)iterator.next();
			product = new Product(productInStock.getDescription(), productInStock.getPrice(), productInStock.getId());
			products.add(product);
		}
		return products;
	}
	
	@Override
	public List<Customer> getListOfCustomers() {
		List liste = entityManager.createQuery( "SELECT p FROM CustomerInDatabase p" ).getResultList();
		List<Customer> customers = new ArrayList<Customer>();
		Iterator iterator = liste.iterator();
		CustomerInDatabase CustomerInDatabase;
		Customer customer;
		while(iterator.hasNext()){
			CustomerInDatabase = (CustomerInDatabase)iterator.next();
			customer = new Customer(CustomerInDatabase.getLastName(), CustomerInDatabase.getFirstName());
			customers.add(customer);
		}
		return customers;
	}

	@Override
	public int order(List<Product> products) {
		
		ProductOrder order = new ProductOrder();
		
		Product product;
		
		for(int i=0; i<products.size(); i++){
			product = products.get(i);
			ProductInStock productInStock = entityManager.find(ProductInStock.class, product.getId());
			//order.addProductInStock(productInStock);
			order.setProduct(productInStock);
			order.setPrice(order.getPrice() + productInStock.getPrice());
		}
		
		EntityTransaction tx = entityManager.getTransaction();
		tx.begin();
		
		entityManager.persist(order);
		
		tx.commit();
		
		return order.getId();
	}

	@Override
	public Order getOrder(int orderIdentifier) {
		ProductOrder productOrder = entityManager.find(ProductOrder.class, orderIdentifier);
		if(productOrder != null){
			// return new Order(productOrder.getId(), productOrder.getPrice(), productOrder.getProduct().size());
		}
		return null;
	}

	@Override
	public void addNewCustomer(CustomerInDatabase customer) throws Exception{
		EntityTransaction tx = entityManager.getTransaction();
		tx.begin();
		
		entityManager.persist(customer);
		
		tx.commit();
	}
	
	@Override
	public int addCustomer(Order order, Customer customer) throws Exception{
		
		ProductOrder productOrder = entityManager.find(ProductOrder.class, order.getId());
		if(productOrder != null){

			CustomerInDatabase customerInDatabase = new CustomerInDatabase(customer.getFirstName(), customer.getLastName());
			customerInDatabase.addProductOrder(productOrder);

			EntityTransaction tx = entityManager.getTransaction();
			tx.begin();
			
			entityManager.persist(customerInDatabase);
			
			tx.commit();
			
			return customerInDatabase.getId();

		}
		
		throw new Exception("order unknown");
	}

	@Override
	public int addCompany(Company company) throws Exception {
		CompanyInDatabase companyInDatabase = entityManager.find(CompanyInDatabase.class, company.getId());
		if(companyInDatabase != null){

			CompanyInDatabase companyToPersist = new CompanyInDatabase(company.getNameCompany(),company.getDescriptionCompany(),company.getSecteurActiviteCompany());
			

			EntityTransaction tx = entityManager.getTransaction();
			tx.begin();
			
			entityManager.persist(companyToPersist);
			
			tx.commit();
			
			return companyToPersist.getIdCompany();

		}
		
		throw new Exception("Company unknown");
	}

}
